#include "changefeaturename.h"
#include "ui_changefeaturename.h"
#include "ukuistylehelper/ukuistylehelper.h"
#include "windowmanager/windowmanager.h"
#include <QDebug>


extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);

ChangeFeatureName::ChangeFeatureName(QStringList featurenames, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ChangeFeatureName),
    names(featurenames)
{
    //去除窗管标题栏
    kdk::UkuiStyleHelper::self()->removeHeader(this);
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);

    ui->logoLabel->setPixmap(QIcon::fromTheme("ukui-control-center").pixmap(QSize(24,24)));
    ui->closeBtn->setToolTip(tr("Close"));
    ui->closeBtn->setProperty("isWindowButton", 0x02);
    ui->closeBtn->setProperty("useIconHighlightEffect", 0x08);
    ui->closeBtn->setFlat(true);
    ui->closeBtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
    connect(ui->closeBtn, &QPushButton::clicked, this, &ChangeFeatureName::close);

    ui->lineEdit->setFocus();

    connect(ui->lineEdit, &QLineEdit::textChanged, this, [=](QString txt){

        if (names.contains(txt)){
            ui->tipLabel->setText(tr("Name already in use, change another one."));
        } else {
            ui->tipLabel->setText(tr(""));
        }


        if (!txt.isEmpty() && ui->tipLabel->text().isEmpty()){
            ui->saveBtn->setEnabled(true);
        } else {
            ui->saveBtn->setEnabled(false);
        }
    });


    connect(ui->cancelBtn, &QPushButton::clicked, [=]{
        close();
    });

    connect(ui->saveBtn, &QPushButton::clicked, [=]{
        emit sendNewName(ui->lineEdit->text());
        close();
    });
}

ChangeFeatureName::~ChangeFeatureName()
{
    delete ui;
}

void ChangeFeatureName::setTitle(QString titleName){
    //setWindowTitle(titleName + tr(" Rename"));
    ui->titleLabel->setText(titleName + tr(" Rename"));
    ui->renameLabel->setText(titleName + tr(" name"));
    QFont font;
    //font.setPixelSize(16);
    QString str = ElideText(font,95,ui->renameLabel->text());
    if(str != ui->renameLabel->text()){
        ui->renameLabel->setToolTip(ui->renameLabel->text());
        ui->renameLabel->setText(str);
    }
}

QString ChangeFeatureName::ElideText(QFont font,int width,QString strInfo)
{
    QFontMetrics fontMetrics(font);
    //如果当前字体下，字符串长度大于指定宽度
    if(fontMetrics.width(strInfo) > width)
    {
        strInfo= QFontMetrics(font).elidedText(strInfo, Qt::ElideRight, width);
    }
    return strInfo;
}

void ChangeFeatureName::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event)

//    QPainter p(this);
//    p.setRenderHint(QPainter::Antialiasing);
//    QPainterPath rectPath;
//    rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 6, 6);

//    // 画一个黑底
//    QPixmap pixmap(this->rect().size());
//    pixmap.fill(Qt::transparent);
//    QPainter pixmapPainter(&pixmap);
//    pixmapPainter.setRenderHint(QPainter::Antialiasing);
//    pixmapPainter.setPen(Qt::transparent);
//    pixmapPainter.setBrush(Qt::black);
//    pixmapPainter.setOpacity(0.65);
//    pixmapPainter.drawPath(rectPath);
//    pixmapPainter.end();

//    // 模糊这个黑底
//    QImage img = pixmap.toImage();
//    qt_blurImage(img, 10, false, false);

//    // 挖掉中心
//    pixmap = QPixmap::fromImage(img);
//    QPainter pixmapPainter2(&pixmap);
//    pixmapPainter2.setRenderHint(QPainter::Antialiasing);
//    pixmapPainter2.setCompositionMode(QPainter::CompositionMode_Clear);
//    pixmapPainter2.setPen(Qt::transparent);
//    pixmapPainter2.setBrush(Qt::transparent);
//    pixmapPainter2.drawPath(rectPath);

//    // 绘制阴影
//    p.drawPixmap(this->rect(), pixmap, pixmap.rect());

//    // 绘制一个背景
//    p.save();
//    p.fillPath(rectPath,palette().color(QPalette::Base));
//    p.restore();
}
